package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelVideoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    public static int type = 0; //0压缩 1提取音频 2拼接 3旋转 4变速 5倒放 6格式转换 7视频转gif 8本地播放 9格式播放
    private String videoPath;
    private SelVideoAdapter selVideoAdapter;
    private boolean again;


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        if (type==8){
            mDataBinding.tvSelVideoTitle.setText(getString(R.string.play_local));
        }
        mDataBinding.ivSelVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selVideoAdapter = new SelVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(selVideoAdapter);
        selVideoAdapter.setOnItemClickListener(this);
        //判断是否是视频拼接再次进入选择视频界面
        again = getIntent().getBooleanExtra(Content.Again, false);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0) {
            VideoCompressActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(VideoCompressActivity.class);
        } else if (type == 1) {
            AudioExtActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(AudioExtActivity.class);
        } else if (type == 2) {
            if (again) {//视频拼接再次选择视频
                Intent intent = new Intent();
                intent.putExtra(Content.VideoPath, selVideoAdapter.getItem(position).getPath());
                setResult(RESULT_OK, intent);
                finish();
                return;
            }
            VideoSpliceActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(VideoSpliceActivity.class);
        } else if (type == 3) {
            VideoRotateActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(VideoRotateActivity.class);
        } else if (type == 4) {
            VideoSpeedActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(VideoSpeedActivity.class);
        } else if (type == 5) {
            VideoUpendActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(VideoUpendActivity.class);
        } else if (type == 6) {
            VideoFormatActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(VideoFormatActivity.class);
        } else if (type == 7) {
            GifConvertActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(GifConvertActivity.class);
        } else if (type == 8) {
            PlayLocalActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(PlayLocalActivity.class);
        }else if (type == 9) {
            PlayFormatActivity.videoPath = selVideoAdapter.getItem(position).getPath();
            startActivity(PlayFormatActivity.class);
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvVideoList.setVisibility(View.GONE);
                        mDataBinding.tvSelVideoNoData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getApplicationContext(), PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    selVideoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}